<!--
*  2021-03-15
*  余纱入库列表
-->
<template>
  <div id="app" class="inbound-surplusYarn">
    <!-- 头部导航 -->
    <van-nav-bar
      title="余纱入库"
      ref="header"
      left-text="返回"
      left-arrow
      @click-left="onClickLeft"
      @click-right="onClickRight"
    >
      <template #right>
        <van-icon name="scan" size="20" />
      </template>
    </van-nav-bar>
    <!-- 总计 -->
    <van-cell
      :title="'标重: ' + weight"
      :label="'沙包数: ' + tableData.length"
      :value="'筒只数: ' + spindleNum"
      class="inbound-surplusYarn__cell"
    ></van-cell>
    <van-cell
      title="默认区域"
      is-link
      :value="storeArea"
      @click="onClickSelectArea"
    ></van-cell>
    <!-- 滚动 -->
    <better-scroll
      class="public-scroll"
      :data="tableData"
      :totalPage="totalPage"
    >
      <van-cell-group v-show="tableData.length !== 0">
        <van-swipe-cell v-for="(data, index) in tableData" :key="index">
          <van-cell :title="'编号: ' + data.standardNo">
            <template slot="label">
              <div class="inbound-surplusYarn-cell">
                <span>
                  <span>色号: </span
                  ><van-field readonly :value="data.colorName"
                /></span>
                <span>
                  <span>规格: </span
                  ><van-field readonly :value="data.standard + '/' + data.strandNum"
                /></span>
                <span>
                  <span>区域:</span>
                  <van-field
                    readonly
                    clickable
                    name="picker"
                    :value="data.storeArea"
                    placeholder="点击选择"
                    @click="onClickSelect(index)"
                  />
                </span>
                <span>
                  <span>重量: </span><van-field readonly :value="data.weight"
                /></span>
                <span>
                  <span>筒管颜色: </span
                  ><van-field readonly :value="data.spindleColor"
                /></span>
                <span>
                  <span>沙包批号: </span
                  ><van-field readonly :value="data.lotNum"
                /></span>
              </div>
            </template>
          </van-cell>
          <template #right>
            <van-button
              square
              type="danger"
              text="删除"
              @click="onDelete(index)"
            />
          </template>
        </van-swipe-cell>
      </van-cell-group>
      <van-empty
        class="custom-image"
        image="https://img01.yzcdn.cn/vant/custom-empty-image.png"
        v-show="tableData.length === 0"
        description="暂无数据"
      />
    </better-scroll>

    <van-cell-group
      class="inbound-surplusYarn-btn"
      v-show="tableData.length !== 0"
    >
      <van-button block type="info" class="bgColor" @click="onClickSubmit"
        >确认入库</van-button
      >
    </van-cell-group>

    <van-popup v-model="showPicker" position="bottom">
      <van-picker
        show-toolbar
        :columns="columns"
        @confirm="onConfirm"
        @cancel="showPicker = false"
      />
    </van-popup>
  </div>
</template>

<script>
import { openWin, comeBack } from "@/utils/index";
import { getYarnPackageFilter, getYarnStoreIn, getByType } from "@/api/yarn";

export default {
  name: "inboundSurplusYarn",
  data() {
    return {
      title: "生产入库",
      currentPage: 1,
      totalPage: 1,
      tableData: [
        // {standardNo: 'xxxx', colorName: 'xxxx',standardName: '10/1', storeArea: '3-A', weight: '50', 'spindleColor': 'xxxx', lotNum: '2'}
      ],
      index: 0,
      showPicker: false,
      columns: [],
      storeArea: "",
      spindleNum: 0,
      weight: 0,
    };
  },
  watch: {
    //  监听入库数据
    tableData: {
      handler(nerArr) {
        this.spindleNum = this.countTotal(nerArr, "spindleNum");
        this.weight = this.countTotal(nerArr, "weight");
      },
      deep: true,
    },
  },
  mounted() {
    const that = this;
    if (process.env.NODE_ENV === "production") {
      this.$nextTick(() => {
        that.initFn(that);
      });
    } else {
      window.apiready = function () {
        that.initFn(that);
      };
    }
  },
  methods: {
    //初始化
    initFn(that) {
      that.onGetTableList();
      window.api.addEventListener({
        name:'viewdisappear'
      }, function() {
        window.api.closeWin();
      });
    },
    // 返回上一页
    onClickLeft() {
      this.storeArea = "";
      comeBack();
    },
    // 扫码
    onClickRight() {
      this.fnOpenFNScanner();
    },
    // 扫码
    fnOpenFNScanner() {
      const that = this;
      const zyHmsScan = api.require('zyHmsScan');
      zyHmsScan.openDefaultView({}, ret => {
        if(ret.status) {
          let content = ret.result;
          if (!that.$isJSON(content) || content.indexOf(",") == -1) {
            that.$toast.fail("二位码异常");
            return false;
          }

          let ids = eval(content);
          if (ids.toString().indexOf(",") == -1) {
            that.$toast.fail("二位码异常");
            return false;
          }
          let id = ids.split(",")[0];
          getYarnPackageFilter(id, (res) => {
            // alert(JSON.stringify(res));
            if (that.storeArea != "") {
              res.storeArea = that.storeArea;
            }
            if (res.refType != 11) {
              that.$toast.fail('非余纱入库')
              return false
            }
            if (that.tableData.length === 0) {
              that.tableData.push(res);
              that.$toast({
                type: "loading",
                message: "请等待",
                duration: 1000,
                onClose: () => {
                  that.fnOpenFNScanner();
                },
              });
            } else {
              let result = false;
              that.tableData.forEach((item) => {
                if (item.id == id) {
                  result = true;
                }
              });
              if (result) {
                that.$toast.fail("请勿重复扫码");
              } else {
                that.tableData.push(res);
                that.$toast({
                  type: "loading",
                  message: "请等待",
                  duration: 1000,
                  onClose: () => {
                    that.fnOpenFNScanner();
                  },
                });
              }
            }
          });
        }
      });
    },
    // 删除
    onDelete(index) {
      this.tableData.splice(index, 1);
    },
    // 配置列表
    onGetTableList() {
      getByType(
        {
          pageSize: 1000,
          type: 9,
        },
        (res) => {
          this.storeArea = res.list[0].propertyValue;
          res.list.forEach((item) => {
            this.columns.push(item.propertyValue);
          });
        }
      );
    },
    // 提交
    onClickSubmit() {
      let datas = [];
      let result = this.tableData.every((item) => item.storeArea != "");
      if (!result) {
        this.$toast.fail("请选择区域");
        return;
      }
      this.tableData.forEach((item) => {
        let params = {};
        params.packageId = item.id;
        params.refType = item.refType;
        params.storeArea = item.storeArea;
        datas.push(params);
      });
      getYarnStoreIn(datas, () => {
        this.$toast.success("入库成功");
        this.$toast({
          type: "success",
          message: "入库成功",
          onClose: () => {
            this.storeArea = "";
            comeBack();
          },
        });
      });
    },
    // 点击保存索引
    onClickSelect(index) {
      this.index = index;
      this.showPicker = true;
    },
    // 计算总重量
    countTotal(arr, keyName) {
      let $total = 0;
      $total = arr.reduce(function (total, currentValue) {
        return currentValue[keyName] ? total + currentValue[keyName] : total;
      }, 0);
      return $total.toFixed(2);
    },
    // 区域选择
    onConfirm(value) {
      if (this.index === "") {
        this.storeArea = value;
        this.showPicker = false;
        return false;
      }
      this.tableData[this.index].storeArea = value;
      this.showPicker = false;
    },
    // 修改默认区域
    onClickSelectArea() {
      this.index = "";
      this.showPicker = true;
    },
    // 查看详情
    onClickDetail() {
      openWin("ordinary-detail", {
        url: window.api.pageParam.url,
        resUrl: window.api.pageParam.resUrl,
      });
    },
  },
};
</script>

<style lang="scss">
.inbound-surplusYarn {
  // padding-top: 22px;
  .van-nav-bar__title {
    .van-button {
      height: 36px;
      line-height: 36px;
    }
  }
  .inbound-surplusYarn__cell {
    .van-cell__title {
      flex: 0 0 67%;
      display: flex;
      justify-content: space-between;
      span,
      .van-cell__label {
        display: flex;
        flex: 1;
      }
      .van-cell__label {
        justify-content: center;
      }
    }
    .van-cell__value {
      flex: 0 0 33%;
    }
    span,
    .van-cell__label {
      font-weight: 900;
      color: #96b327;
      font-size: 14px;
    }
  }
  .public-scroll {
    height: calc(100vh - 234px);
    overflow: hidden;
    .inbound-surplusYarn-cell {
      display: flex;
      flex-wrap: wrap;
      padding-top: 0;
      & > span {
        display: flex;
        width: 50%;
        height: 30px;
        align-items: center;
        & > span {
          width: 100px;
        }
        .van-cell.van-field {
          padding-left: 0;
        }
      }
    }
  }
  .inbound-surplusYarn-btn {
    padding: 16px;
    &.van-hairline--top-bottom::after,
    .van-hairline-unset--top-bottom::after {
      border-width: 0;
    }
  }
  .van-swipe-cell__right {
    .van-button {
      height: 100%;
    }
  }
}
</style>
